(function () {

	//获取显示页面数据 服务器分页
    var serverData = {};
    var serverTable;
    jQuery(document).ready(function () {

        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/salemanMobilize/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                	data: null
                },
                {
                    data: 'salemanName'
                },
                {
                    data: 'gender'
                },
                {
                    data: 'birday',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
                },
                {
                    data: 'position'
                },
                {
                    data: 'contractPhone'
                },
                {
                    data: 'memo'
                },
                {
                    data: 'saleArea'
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate ='<button type="button" class="btn btn-sm btn-info editBtn"><i class="fa fa-pencil"></i></button>'
                            +'<button type="button" class="btn btn-sm btn-info delBtn"><i class="fa fa-remove"></i></button>';

                        return operate;

                    }
                }],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();

                    if (target.className.indexOf('fa-remove') != -1 || target.className.indexOf('delBtn') != -1) {

                        deleteModule(data);
                    }

                    if (target.className.indexOf('fa-pencil') != -1 || target.className.indexOf('editBtn') != -1) {

                        editModule(data);
                    }

                    return;
                });
            }
        });
        var deleteModule = function (data) {
            if (!data) return;
            $.springLemonDialog({
                content: '确认账号：' + data.salemanName + '？',//确认文本
                clickButton: function (sender, modal, index) {//按钮事件处理
                    //index:0 确认    1：取消
                    //sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
                    if (index == 0) {
                        var url =window.rootPath+ "/api/salemanMobilizes/" + data.id;
                        jQuery.ajax({
                            url: encodeURI(url),
                            cache: false,
                            type: "delete",
                            dataType: "json",
                            success: function (data) {
                                // serverTable.draw(false);
                                window.location = window.rootPath+"/ui/salemanMobilize/";
                            }
                        });
                    }
                    $(this).closeDialog(modal);
                }
            });
        };
        
        //添加销售人员属性，弹窗清空
    	jQuery('.addBtn').on('click', function () {
    		jQuery('#id').val('');
    		jQuery('#salemanName').val('');
    		jQuery('#gender').val('');
    		jQuery('#birday').val('');
    		jQuery('#position').val('');
    		jQuery('#contractPhone').val('');
    		jQuery('#memo').val('');
    		jQuery('#saleArea').val('');
    		jQuery('#salemanMobilizeModal').modal('show');
    	});
    	
    	//编辑销售人员属性，添加属性
        var editModule=function(data){
            if(!data) return;
            if (data.gender == "男") {
                var sex="male";
            }
            if (data.gender == "女") {
            	var sex="female";
            }
            $(":radio[name='gender'][value='" + sex + "']").prop("checked", "checked");
            jQuery('#id').val(data.id);
            jQuery('#salemanName').val(data.salemanName);
            jQuery('#birday').val(moment(data.birday).format("YYYY-MM-DD"));
            jQuery('#position').val(data.position);
            jQuery('#contractPhone').val(data.contractPhone);
            jQuery('#memo').val(data.memo);
            jQuery('#saleArea').val(data.saleArea);
            jQuery('#salemanMobilizeModal').modal('show');
        };
    	
    	//添加/编辑销售人员属性，保存
    	var $btn = jQuery('.saveBtn').on('click', function () {
            if(jQuery("#salemanMobilizeAtrrForm") && !jQuery("#salemanMobilizeAtrrForm").Validform({tiptype:3, showAllError:true}).check(false)){
                return;
            };
            var isId=jQuery("#id").val();
            //判断是编辑还是新增
            if(isId == ""){
            	var salemanName = jQuery('#salemanName').val();
            	var option = $("input[type='radio']:checked").val();
                if (option == "male") {
                    jQuery("#gender").val("男");
                }
                if (option == "female") {
                    jQuery("#gender").val("女");
                }
             	var gender = jQuery('#gender').val();
         		var birday = jQuery('#birday').val();
         		var position = jQuery('#position').val();
         		var contractPhone = jQuery('#contractPhone').val();
         		var memo = jQuery('#memo').val();
         		var saleArea = jQuery('#saleArea').val();
                var api = window.rootPath + "/api/salemanMobilizes";
                var d = {
                	"salemanName": salemanName,
                    "gender": gender,
                    "birday": birday,
                    "position": position,
                    "contractPhone": contractPhone,
                    "memo": memo,
                    "saleArea":saleArea
                };
                jQuery.ajax({
                    type: 'post',
                    url: api,
                    data: JSON.stringify(d),
                    dataType: 'json',
                    contentType: 'application/json;charset=utf-8',
                    success: function (data) {
                    	jQuery('#salemanMobilizeModal').modal('hide');
                        serverTable.draw(false);
                    }
                });

            }
            else{
            	var option = $("input[type='radio']:checked").val();
                if (option == "male") {
                    jQuery("#gender").val("男");
                }
                if (option == "female") {
                    jQuery("#gender").val("女");
                }
            	var data={
                        id:jQuery('#id').val(),
                        salemanName:jQuery('#salemanName').val(),
             			gender:jQuery('#gender').val(),
         				birday:jQuery('#birday').val(),
         				position:jQuery('#position').val(),
         				contractPhone:jQuery('#contractPhone').val(),
         				memo:jQuery('#memo').val(),
         				saleArea:jQuery('#saleArea').val()
                    };
                    var url=window.rootPath+'/api/salemanMobilizes/'+data.id;
                    jQuery.ajax({
                        url:encodeURI(url),
                        type:'put',
                        data:JSON.stringify(data),
                        dataType:'json',
                        contentType:'application/json;charset=utf-8',
                        success: function () {
                            jQuery('#salemanMobilizeModal').modal('hide');
                            serverTable.draw(false);
                        }

                    });
            }
        });
        
    	//查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        //撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });


    });
})();